package com.easemob.chatuidemo.activity;

import com.easemob.chatuidemo.R;
import com.easemob.chatuidemo.domain.User;
import com.easemob.chatuidemo.service.UserService;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ContactDetailsActivity extends BaseActivity {
    protected ImageLoader imageLoader = ImageLoader.getInstance();
    /**
     * 头像
     */
    private ImageView iv_contact_head;
    /**
     * 姓名/昵称
     */
    private TextView tv_contactname;
    /**
     * ID
     */
    private TextView tv_contact_id;
    /**
     * 地区
     */
    private TextView tv_contact_location;
    /**
     * 电话
     */
    private TextView tv_contact_phone;
    
    private String uid;
    private ProgressDialog progressDialog;
    private User userInfos;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_details);
        Intent intent = new Intent();
        uid = getIntent().getStringExtra("uId");
        initView();
    }
    
    public void initView(){
        iv_contact_head = (ImageView) findViewById(R.id.iv_contact_detalis);
        tv_contactname = (TextView) findViewById(R.id.tv_contact_details_name);
        tv_contact_id = (TextView) findViewById(R.id.tv_contact_details_ID);
        tv_contact_location = (TextView) findViewById(R.id.tv_contact_details_local);
        tv_contact_phone = (TextView) findViewById(R.id.tv_contact_details_phone);
        
        new getAccountNetWork().execute(uid);
    }
    
    
    
    public void sendMSG(View view) {
        startActivity(new Intent(ContactDetailsActivity.this,
                ChatActivity.class).putExtra("userId", userInfos.getUsername())
                .putExtra("userName", userInfos.getNickname()));
    }

    public void video(View view) {
        startActivity(new Intent(ContactDetailsActivity.this, VideoCallActivity.class).putExtra("username", userInfos.getUsername())
                .putExtra("isComingCall", false));
    }

    public void toCall(View view) {
        String Telphone=userInfos.getSendTel();
        Uri uri=Uri.parse("tel:"+Telphone);  
        Intent intent=new Intent();  
        intent.setAction(Intent.ACTION_CALL);  
        intent.setData(uri);  
        ContactDetailsActivity.this.startActivity(intent);
    }
    
    void progressDialogInit(){
        progressDialog = new ProgressDialog(this);
        progressDialog.setIcon(android.R.drawable.ic_menu_info_details);
        progressDialog.setTitle("正在获取数据");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
        progressDialog.setMessage("请稍后...");
    }
    
    public void showDialog(String msg){
        AlertDialog.Builder builder = new Builder(ContactDetailsActivity.this);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setMessage(msg);
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
    
    class getAccountNetWork extends AsyncTask<String, Integer, User>{

        @Override
        protected void onPreExecute() {
            progressDialogInit();
            progressDialog.show();
            super.onPreExecute();
        }

        @Override
        protected User doInBackground(String... params) {
            UserService userService = new UserService();
            userInfos = userService.queryUserInfo(params[0]);
            return userInfos;
        }

        @Override
        protected void onPostExecute(User result) {
            if (result == null) {
                progressDialog.dismiss();
                showDialog("网络连接失败！");
                return;
            }
            if (!"0000".equals(result.getRetCode())) {//后台查询出错
                showDialog("后台查询出错！");
                return;
            }
            progressDialog.dismiss();
            tv_contactname.setText(result.getNickname());
            tv_contact_id.setText(result.getLoginname());
            tv_contact_phone.setText(result.getSendTel());
            tv_contact_location.setText(result.getAddress());
            imageLoader.displayImage(result.getUpic(), iv_contact_head);
            super.onPostExecute(result);
        }

    }

}
